﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;

namespace System.Collections.Observable.Transaction
{
    class PendingCollectionChangedEventList: IEnumerable<PendingCollectionChangedEvent>
    {
        LinkedList<PendingCollectionChangedEvent> LinkedList = new LinkedList<PendingCollectionChangedEvent>();

        public void Add(NotifyCollectionChangedEventArgs pendingEvent)
        {
            //if list is empty, add first element
            if (LinkedList.Last == null)
            {
                LinkedList.AddLast(new PendingCollectionChangedEvent(pendingEvent));
            }
            //if can't merge, add element at the end
            else if (LinkedList.Last.Value.TryMerge(pendingEvent) == false)
            {
                LinkedList.AddLast(new PendingCollectionChangedEvent(pendingEvent));
            }
        }

        public IEnumerator<PendingCollectionChangedEvent> GetEnumerator()
        {
            return LinkedList.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)LinkedList).GetEnumerator();
        }
    }
}
